package yassp.client;


import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Milridor
 */
public class File {
    public File(String filePath, String hash) throws FileNotFoundException, IOException, NoSuchAlgorithmException {
        
        this.hash = hash;
        
        java.io.File inputFile = new java.io.File(filePath);
        java.io.File outputFile = new java.io.File("C:\\testYassp\\" + hash);
        if(outputFile.exists())
            return;

        FileReader in = new FileReader(inputFile);
        FileWriter out = new FileWriter(outputFile);
        char[] c = null;
        int count;

        count = in.read(c,0,2^12);
        while(count != -1) {
            out.write(c);
            count = in.read(c,0,2^12);
        }
        
        in.close();
        out.close();
        
    }
    
    public File(String hash) throws IOException {
        
        this.hash = hash;
    }
    
    public static String makeHash(String filePath) throws IOException, NoSuchAlgorithmException{
        java.io.File inputFile = new java.io.File(filePath);
  
        FileReader in = new FileReader(inputFile);
        MessageDigest md = MessageDigest.getInstance("SHA-1");
        char[] c = null;
        int count;

        count = in.read(c,0,2^12);
        while(count != -1) {
            md.update(String.valueOf(c).getBytes("UTF-8"));
            count = in.read(c,0,2^12);
        }
        
        in.close();
        
        return md.digest().toString();
    }
    
    // Read from the stream and write it to a file
    public void deserialize(PipedInputStream stream) {
        try {
            java.io.File outputFile = new java.io.File("C:\\testYassp\\" + hash);
            FileOutputStream output;
            output = new FileOutputStream(outputFile);
            
            byte[] buffer = new byte[256];
            
            while(stream.read(buffer) != -1)
                output.write(buffer);
            
            output.close();
        } catch (IOException ex) {
            Logger.getLogger(File.class.getName()).log(Level.SEVERE, null, ex);
        }
            
        
    }
    
    public void serialize(PipedOutputStream stream) throws FileNotFoundException, IOException {
        java.io.File f = new java.io.File("C:\\testYassp\\" + hash);
        DataOutputStream dout = new DataOutputStream(stream);
        dout.writeUTF(hash);
        dout.writeLong(getSize());
        dout.close();
        FileReader fr = new FileReader(f);
        int c;
        //TODO: Lire plusieurs octets
        while ((c = fr.read()) != -1)
            stream.write(c);
        
        fr.close();
    }
    
    public void write(String destPath) throws FileNotFoundException, IOException {
        java.io.File destFile = new java.io.File(destPath);
        java.io.File sourceFile = new java.io.File("C:\\testYassp\\" + hash);
        if(destFile.exists()) {
            destFile.delete();
            destFile.createNewFile();
        }

        FileReader in = new FileReader(sourceFile);
        FileWriter out = new FileWriter(destFile);
        int c;

        while ((c = in.read()) != -1)
            out.write(c);

        in.close();
        out.close();
    }
    
    public long getSize() {
        java.io.File f = new java.io.File("C:\\testYassp\\" + hash);
        return f.length();
    }
    
    String hash;
    
}